package com.example.aijie.autoreadhelper.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.math.BigDecimal;
import java.util.UUID;

/**
 * 设备相关工具类
 */
public class DeviceHelper {
    private DeviceHelper() {
    }

    /**
     * 得到设备的屏幕密度
     *
     * @param context
     * @return
     */
    public static float getScreenDensity(@NonNull Context context) {
        return context.getResources().getDisplayMetrics().density;
    }

    /**
     * dip转为 px
     *
     * @param context
     * @param dipValue
     * @return
     */
    public static int dip2px(@NonNull Context context, float dipValue) {
        final float value = dipValue * getScreenDensity(context) + 0.5f;
        return new BigDecimal(value).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
    }

    /**
     * 将sp值转换为px值
     *
     * @param context
     * @param spValue
     * @return
     */
    public static int sp2px(Context context, float spValue) {
        final float value = spValue * getScreenDensity(context) + 0.5f;
        return new BigDecimal(value).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();

    }

    /**
     * px 转为 dip
     *
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2dip(@NonNull Context context, float pxValue) {
        final float value = pxValue / getScreenDensity(context) + 0.5f;
        return new BigDecimal(value).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
    }


    /**
     * 应用是否安装
     *
     * @param context
     * @param packageName 包名
     * @return
     */
    public static boolean isInstall(@NonNull Context context, String packageName) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
            return info != null;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * AndroidID,这个ID会改变如果进行了出厂设置。并且，如果某个Andorid手机被Root过的话，这个ID也可以被任意改变。
     *
     * @param context
     * @return
     */
    @SuppressLint("HardwareIds")
    public static String getAndroidID(@NonNull Context context) {
        return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    /**
     * Wifi物理地址
     *
     * @param context
     * @return
     */
    @SuppressLint("HardwareIds")
    public static String getMacAddress(@NonNull Context context) {
        WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wm != null) {
            return wm.getConnectionInfo().getMacAddress();
        }
        return "";
    }

    /**
     * ROM版本、制造商、CPU型号、以及其他硬件信息
     *
     * @return
     */
    public static String getUniquePseudoID() {
        String m_szDevIDShort = "35" +
                (Build.BOARD.length() % 10) +
                (Build.BRAND.length() % 10) +
                (Build.CPU_ABI.length() % 10) +
                (Build.DEVICE.length() % 10) +
                (Build.MANUFACTURER.length() % 10) +
                (Build.MODEL.length() % 10) +
                (Build.PRODUCT.length() % 10);

        String serial;
        try {
            serial = Build.class.getField("SERIAL").get(null).toString();
            return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        } catch (Exception e) {
            serial = "serial";
        }
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    }

    public static int getScreenHeight(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metric);
        return metric.heightPixels;
    }

    public static int getScreenWidth(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metric);
        return metric.widthPixels;
    }
}
